package cn.jinbyte.data.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

@Data
public class PageReq {
    /**
     * 当前页码
     */
    @Nullable
    private Integer page = 0;
    /**
     * 每页数量
     */
    @Nullable
    private Integer pageSize = 10;
    /**
     * 排序对象
     */
    @JsonIgnore
    private Sort sort;

    /**
     * 获取分页对象
     *
     * @return 分页对象
     */
    @NotNull
    public PageRequest pageRequest() {
        page = page == null ? 0 : page;
        pageSize = pageSize == null ? 10 : pageSize;
        PageRequest pageRequest = PageRequest.of(page, pageSize);

        if (sort != null) {
            pageRequest = pageRequest.withSort(sort);
        }
        return pageRequest;
    }

}
